package Examples;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.hadoop.io.FloatWritable;

import homomorphism.list.ListHomomorphism;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import utility.Tuple;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author takeshi
 * not finished
 */
public class Variance extends ListHomomorphism<FloatWritable, Tuple<Float, Float, Integer> > {
     private static final Log LOG = LogFactory.getLog(Variance.class);
    // data number cannot lager than 2^32
    @Override
    public Tuple<Float, Float, Integer> filter(FloatWritable val) {
        final float v = val.get();
        Tuple<Float, Float, Integer> interTuple = new Tuple<Float, Float, Integer>();
        interTuple.Set1st(v * v);
        interTuple.Set2nd(v);
        interTuple.Set3rd(1);
        return interTuple;
    }

    @Override
    public Tuple<Float, Float, Integer> plus(Tuple<Float, Float, Integer> a, Tuple<Float, Float, Integer> b) {
        if(a == null || b == null) throw new NullPointerException("input can not be null!");
//        Tuple<Float, Float, Integer> rsttttt = new Tuple<Float, Float, Integer>(1f, 1f, 1);
//        return rsttttt;
        float fst = a.Get1st() + b.Get1st();
        float snd = a.Get2nd() + b.Get2nd();
        int thd = a.Get3rd() + b.Get3rd();
        Tuple<Float, Float, Integer> rst = new Tuple<Float, Float, Integer>(fst, snd, thd);
        return rst;
    }
    @Override
    public Object GetValue( Tuple<Float, Float, Integer> val) {
        Tuple<Float, Float, Integer> tmp =  new Tuple<Float, Float, Integer>(val);
        int n = tmp.Get3rd();
        int n2 = n*n;

        return tmp.Get1st()/n - tmp.Get2nd()*tmp.Get2nd()/n2;
    }
}
